package com.bridgeintelligent.tag.scene.controller;

import com.bridgeintelligent.tag.scene.api.ISceneCatalogService;
import com.bridgeintelligent.tag.scene.api.ISceneService;
import com.bridgeintelligent.tag.scene.api.IUploadService;
import com.bridgeintelligent.tag.scene.api.dto.SceneDto;
import com.bridgeintelligent.tag.scene.api.dto.ScenePage;
import com.bridgeintelligent.tag.scene.api.entity.Scene;
import com.bridgeintelligent.tag.scene.api.model.SceneModel;
import com.bridgeintelligent.tag.scene.api.model.SceneMoveModel;
import com.bridgeintelligent.tag.scene.api.model.SceneQueryModel;
import com.bridgeintelligent.tag.scene.api.vo.*;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * @description
 * @author: grp 2021-01-07 11:15
 **/

@Api(tags = "场景")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("api/scene")
public class SceneController {

    private ISceneService sceneService;
    private ISceneCatalogService sceneCatalogService;
    private IUploadService uploadService;

    @ApiOperation(value = "场景查询-按名称", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "sceneName", value = "场景名称")})
    @GetMapping("inCatalogList")
    public SceneDto<List<String>> inCatalogList(String sceneName) {
        List<String> result = sceneService.inCatalogList(sceneName);
        return SceneDto.success(result);
    }

    @ApiOperation(value = "场景移动", notes = "场景管理")
    @PostMapping("update")
    public SceneDto<Object> update(@RequestBody Scene scene) {
        return sceneService.update(scene);
    }

    @ApiOperation(value = "场景查询-按目录", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "目录id", required = true)})
    @GetMapping("sceneList")
    public SceneDto<List<Scene>> sceneList(@Valid @NotBlank String id) {
        List<Scene> scenes = sceneService.findByCatalogId(id);
        return SceneDto.success(scenes);
    }

    @ApiOperation(value = "新增场景", notes = "场景管理")
    @PostMapping("add")
    public SceneDto<Object> add(@RequestBody @Valid SceneModel model) {
        int count = sceneService.countByName(model.getBase().getName());
        if(count>0){
            return SceneDto.error("场景名称重复，请修改场景名称后重新提交！");
        }
        sceneService.add(model);
        return SceneDto.success();
    }

    @ApiOperation(value = "客群", notes = "场景管理")
    @GetMapping("customers")
    public SceneDto<List<CustomersVO>> customers() {
        List<CustomersVO> customers = sceneService.findCustomers();
        return SceneDto.success(customers);
    }

    @ApiOperation(value = "组合标签", notes = "场景管理")
    @GetMapping("combinations")
    public SceneDto<List<CombinationTagVO>> combinations() {
        List<CombinationTagVO> combinations = sceneService.findCombinations();
        return SceneDto.success(combinations);
    }

    @ApiOperation(value = "定制标签", notes = "场景管理")
    @GetMapping("frees")
    public SceneDto<List<FreeTagVO>> frees() {
        List<FreeTagVO> frees = sceneService.findFrees();
        return SceneDto.success(frees);
    }

    @ApiOperation(value = "修改场景", notes = "场景管理")
    @PostMapping("put")
    public SceneDto<Object> put(@RequestBody @Valid SceneModel model) {
        if(StringUtils.isBlank(model.getBase().getId())){
            return SceneDto.error("场景id不能为空！");
        }
        int count = sceneService.countByNameAndNotId(model.getBase().getName(),model.getBase().getId());
        if(count>0){
            return SceneDto.error("场景名称重复，请修改场景名称后重新提交！");
        }
        sceneService.put(model);
        return SceneDto.success();
    }

    @ApiOperation(value = "场景列表", notes = "场景管理")
    @GetMapping("list")
    public SceneDto<ScenePage<SceneVO>> list(@ModelAttribute @Valid SceneQueryModel model) {
        model.setPageNumber(model.getPageNumber() == null ? 1 : model.getPageNumber());
        model.setPageSize(model.getPageSize() == null ? 10 : model.getPageSize());
        ScenePage<SceneVO> page = sceneService.list(model);
        return SceneDto.success(page);
    }

    @ApiOperation(value = "置顶", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "场景id", required = true)})
    @PostMapping("top")
    public SceneDto<Object> top(@NotBlank String id) { ;
        return sceneService.top(id);
    }
    @ApiOperation(value = "上移", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "场景id", required = true)})
    @PostMapping("up")
    public SceneDto<Object> up(@ModelAttribute @Valid SceneMoveModel model) {
        return sceneService.up(model);
    }
    @ApiOperation(value = "下移", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "场景id", required = true)})
    @PostMapping("down")
    public SceneDto<Object> down(@ModelAttribute @Valid SceneMoveModel model) {
        return sceneService.down(model);
    }

    @ApiOperation(value = "场景删除", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "场景ID", required = true)})
    @DeleteMapping("del")
    public SceneDto<Object> del(@NotBlank String id) {
        sceneService.del(id);
        return SceneDto.success();
    }

    @ApiOperation(value = "场景详情", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "场景id", required = true)})
    @GetMapping("detail")
    public SceneDto<SceneDetailVO> detail(@NotBlank String id) {
        return SceneDto.success(sceneService.getDetail(id));
    }

    @ApiOperation(value = "目录列表", notes = "场景管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "场景id")})
    @GetMapping("catalogs")
    public SceneDto<List<SceneCatalogVO>> catalogs(String id) {
        List<SceneCatalogVO> result = sceneCatalogService.catalogs(id);
        return SceneDto.success(result);
    }

    @ApiOperation(value = "目录场景树", notes = "场景管理")
    @GetMapping("tree")
    public SceneDto<List<SceneCatalogVO>> catalogs() {
        List<SceneCatalogVO> result = sceneCatalogService.tree();
        return SceneDto.success(result);
    }

    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType="MultipartFile", name = "images", value =
            "图片列表")})
    @PostMapping(path="upload")
    public SceneDto uploadEditorImg(@RequestParam("images") List<MultipartFile> list, HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        String urlPre = url.replace("upload", "img");
        List<String> urls = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(img->{
                String fileName = uploadService.upload(img);
                urls.add(urlPre+"/"+fileName);
                log.info("url:{}",urlPre+"/"+fileName);
            });
        }
        return SceneDto.success(urls);
    }

    @ApiOperation(value = "目录列表", notes = "场景管理")
    @GetMapping("img/{name}")
    public void img(@ApiParam(name = "name", value = "图片名称", required = true)
                        @PathVariable @NotBlank String name, HttpServletRequest request,
                    HttpServletResponse response) {
        uploadService.download(name.split("\\.")[0],request,response);
    }
}
